package com.banmajio.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    private static final int MIN_AGE = 3;
    private static final int MAX_AGE = 85;
    // 时间格式 yyyy-MM-dd
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    // 时间格式 yyyy-MM-dd HH:mm:ss
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 根据出生日期计算年龄
     *
     * @param birthDay 出生日期
     * @return 年龄
     */
    public static int getAgeByBirth(Date birthDay) {
        if (birthDay == null) return 0;
        LocalDate currentDate = LocalDate.now();
        LocalDate birthDate = LocalDateTime.ofInstant(birthDay.toInstant(), ZoneId.systemDefault()).toLocalDate();
        Period age = Period.between(birthDate, currentDate);
        return age.getYears() + ((age.getMonths() > 0 || age.getDays() > 0) ? 1 : 0);
    }

    /**
     * 获取月+日
     *
     * @return 返回格式：“0801"
     */
    public static String getMouthDate() {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("MMdd");
        return LocalDateTime.now().format(format);
    }

    /**
     * 返回前一天的年+月
     *
     * @return 返回格式：“202401"
     */
    public static String getPreviousDayMouthYearStr() {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDate currentDate = LocalDate.now();
        LocalDate previousDay = currentDate.minusDays(1);
        return previousDay.format(format);
    }

    /**
     * 获取前一天的年份
     *
     * @return 返回格式：“2024"
     */
    public static String getPreviousYearStr() {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy");
        LocalDate currentDate = LocalDate.now();
        LocalDate previousDay = currentDate.minusDays(1);
        return previousDay.format(format);
    }

    /**
     * 获取年+月+日
     *
     * @return 返回格式：“20230801"
     */
    public static String getYearMonthDay() {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMdd");
        return LocalDateTime.now().format(format);
    }

    /**
     * 获取年-月份+日期
     *
     * @return 返回格式：“2023-08-01"
     */
    public static String getYearMonthDayDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取年月日+时分秒
     *
     * @return 返回格式：“2023-08-01 12:18:17"
     */
    public static String getYearMonthDayTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(new Date());
    }

    /**
     * 字符串转date日期
     *
     * @return date
     */
    public static Date getStringToDate(String s) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = format.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 字符串转date时间
     *
     * @return date
     */
    public static Date getStringToTime(String s) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date parse = null;
        try {
            parse = format.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * string格式转化为Date对象
     */
    public static Date format(String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    /**
     * Date对象转string格式
     */
    public static String format(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date.getTime());
    }

    public static String normalFormat(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_PATTERN);
        return formatter.format(date.getTime());
    }

    /**
     * Date对象转string格式斜杠
     */
    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd/");
        return formatter.format(date.getTime());
    }

    /**
     * 年龄限制在3~85岁之间
     */
    public static boolean isValidAge(LocalDate birthDate) {
        LocalDate currentDate = LocalDate.now();
        Period age = Period.between(birthDate, currentDate);
        int years = age.getYears();
        return years >= MIN_AGE && years <= MAX_AGE;
    }

    /**
     * 将分钟转换成小时
     * @param minute 分钟数
     * @return 小时
     */
    public static String minutesToHours(Long minute) {
        double result = (double) minute / 60;
        // 使用DecimalFormat类来格式化结果为两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        String format = df.format(result);
        if (format.equals("0.00")){
            return "0";
        }
        return format;
    }

    public static Date formatYMD(String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_PATTERN);
        Date date = null;
        try {
            date = formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }
}
